package com.supergdtop.www.run.tools;

import android.content.Context;

import java.util.Timer;
import java.util.TimerTask;

/**
 * 双击返回键退出工具类
 * Created by Asum on 2016/7/13 0013.
 */
public class QuitForDoubleTouchTools {
    private Context context;
    private Timer timer;

    private boolean isFirstTouch = true;
    private QuitForDoubleTouchToolsCallBack callBack;

    public QuitForDoubleTouchTools(Context context) {
        this.context = context;
    }

    public void touchBack() {
        if (!isFirstTouch) {
            if (timer != null) {
                timer.cancel();
            }

            if (callBack != null) {
                callBack.quit();
            }
        } else {
            if (timer != null) {
                timer.cancel();
            }
            timer = new Timer();
            TimerTask task = new TimerTask() {
                public void run() {
                    isFirstTouch = true;
                    timer.cancel();
                }
            };
            timer.schedule(task, 2650, 2650);
            isFirstTouch = false;
            if (callBack != null) {
                callBack.hold();
            }
        }
    }

    public void setCallBack(QuitForDoubleTouchToolsCallBack callBack) {
        this.callBack = callBack;
    }

    public interface QuitForDoubleTouchToolsCallBack {
        public void quit();

        public void hold();
    }
}
